package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;

import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;

import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/system/user")//一级路径
public class UserController extends BaseController {



    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list",name = "进入用户管理页面")
    public String findAll(@RequestParam(name = "page",defaultValue = "1") Integer pageNum,
                          @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize){

        PageInfo<User> pageInfo = userService.findByPage(getCompanyId(),pageNum, pageSize);
        request.setAttribute("page",pageInfo);
        //int i=1/0;
        return "system/user/user-list";
    }

    @RequestMapping(value = "/toAdd",name = "进入新增页面")
    public String toAdd(User user){
        //查询本企业部门输入
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);


        //直接转发到用户添加页面
        return "system/user/user-add";
    }

    @RequestMapping(value ="/edit",name = "保存用户")
    public String save(User user){
        //判断传递过来的参数是否有id的值   没有就保存save   有更新update
        if (StringUtils.isEmpty(user.getId())) {
            user.setId(UUID.randomUUID().toString());
            user.setCompanyName(getCompanyName());
            user.setCompanyId(getCompanyId());
            user.setCreateTime(new Date());  //数据创建时间  为了排序
            userService.save(user);
        }else{
            userService.update(user);
        }

        //直接重定向到用户添加页面
        return "redirect:/system/user/list.do";
    }


    @RequestMapping(value = "/toUpdate",name = "跳转到编辑页面（编辑和新增同一个页面）")
    public String toUpdate(String id){
        //数据回显
        User user = userService.findById(id);
        //查询本企业部门输入
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);
        request.setAttribute("user",user);
        //直接转发到编辑页面
        return "system/user/user-add";
    }

    @RequestMapping(value = "/delete",name = "删除用户")
    public String delete(String id){//用户id
        //删除用户之前先删除用户角色
        userService.deleteUserAndRole(id);
        //删除用户
        userService.deleteById(id);

        //查询本企业部门输入
        List<Dept> deptList = deptService.findAll(getCompanyId());

        request.setAttribute("deptList",deptList);

        //直接转发到编辑页面
        return "redirect:/system/user/list.do";
    }


    @RequestMapping(value = "/roleList",name = "跳转到编辑角色页面")
    public String roleList(String id){//用户id
        //当前用户对象
        User user = userService.findById(id);
        request.setAttribute("user",user);

        //查询当前登录人的企业的所有角色
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList",roleList);

        //查询当前用户觉得 拼成字符串  用于数据回显
        List<String> roleIds =  roleService.findRoleIdsByUserId(id);

        String userRoleStr = roleIds.stream().collect(Collectors.joining(","));
        request.setAttribute("userRoleStr",userRoleStr);

        return "system/user/user-role";
    }



    @RequestMapping(value = "/changeRole",name = "给用户分配角色id")
    public String changeRole(String userid,String[]roleIds){//用户id
        //向角色中间表中插入数据
        userService.changeRole(userid,roleIds);

        return "redirect:/system/user/list.do";
    }


  /* public static void main(String[] args) {

        String password1 = new Md5Hash("123456", "xiaowang@export.com", 2).toString();


        System.out.println(password1);

    }*/
}
